package collections;

import java.util.*;

class Set_TreeSetTest {
    public static void main(String[] args) {
        TreeSet<String> treeSet = new TreeSet<String>();
        treeSet.add("苹果");
        treeSet.add("谷歌");
        treeSet.add("微软");
        treeSet.add("MOTO");

        System.out.println("迭代器遍历TreeSet：");
        Iterator<String> it = treeSet.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        
        System.out.println("增强for遍历TreeSet：");
        for (String string : treeSet) {
            System.out.println(string);
        }
        System.out.println("先转换成数组再遍历：");
        Object[] stringArray = treeSet.toArray();
        for (Object object : stringArray) {
            System.out.println(object);
        }
        for (int i = 0; i < stringArray.length; i++) {
            System.out.println(stringArray[i]);
        }
    }
}

class Student implements Comparable {
    int num;
    String name;

    static class StudentComparator implements Comparator {
        public int compare(Object o1, Object o2) {
            Student s1 = (Student) o1;
            Student s2 = (Student) o2;
            int result = s1.num > s2.num ? 1 : (s1.num == s2.num ? 0 : -1);
            if (result == 0) {
                result = s1.name.compareTo(s2.name);
            }
            return result;
        }
    }

    Student(int num, String name) {
        this.num = num;
        this.name = name;
    }

    public int compareTo(Object o) {
        Student s = (Student) o;
        return num > s.num ? 1 : (num == s.num ? 0 : -1);
    }

    public String toString() {
        return num + ":" + name;
    }
}